/*
 * table_buf_CppDirectiveInfo.c
 *
 *  Created on: 2012/03/31
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"

/* KIND_DIRECTIVEINFO	*/
typedef struct _DIRECTIVEINFO
{
	U4		ulKind;	/* 2 */
	S4		lineno;        					/* #line */
	union{
		AST		astPPCondition;  				/* #if, #elif */
		AST		astHeader;						/* #include */
		AST		astMacroName;  				/* #ifdef,#ifndef */
		AST		astErrorMsg;				 		/* #error */
		AST		astPragmaCmd;  				/* #pragma */
	}unParam;					/* 3 */
	union{
	    BOOL		ulTrueFalse;						/* #if, #elif,#ifdef,#ifndef  */
		AST		astFileIndex;						/* #include *//* #line */
		AST		astMacroDefInfo;  				/* #define */
	}unExeResult;			/* 4 */
	AST			astLocation;	/* 5 */
	AST         astLocScope;
}DIRECTIVEINFO, *PDIRECTIVEINFO;

#define TableSize  (sizeof(DIRECTIVEINFO))
#define TableType  PDIRECTIVEINFO
#define stTable stDirectiveTblManager.memoryBlocks
#define maxTable stDirectiveTblManager.maxRecordNum
#define  maxNumPerBlk  (stDirectiveTblManager.astMaxNum_Block)

#define BufType  PSZ
#define Buffer stDirectiveBufManager.memoryBlocks
#define Buflen stDirectiveBufManager.maxRecordNum
#define  bufLenPerBlk  (stDirectiveBufManager.astMaxNum_Block)

/* private field */

/* get the object */
static TABLEMANAGER stDirectiveBufManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		1,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};
static TABLEMANAGER stDirectiveTblManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		TableSize,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};
static VOID resetBufListener( U4  procid, U2 index )
{
	if( procid == stDirectiveBufManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			Buffer[ index-1] = NULL;
		}
	}
}
static BufType core_getBuf( AST index )
{
	BufType temp = Check_Com( &stDirectiveBufManager, resetBufListener, index );
	if( temp != NULL ){
		return &temp[index%bufLenPerBlk];
	}
	return NULL;
}
static BOOL AllocBuffer( AST astReqArg )
{
	return AllocBuffer_Com( &stDirectiveBufManager, astReqArg, resetBufListener );
}
//		BOOL (*clear)( VOID );
static BOOL ClearBuffer( VOID )
{
	return Clear_Com( &stDirectiveBufManager );
}
static BOOL ResetBuffer( VOID )
{
	return Reset_Com( &stDirectiveBufManager );
}
static VOID resetListener( U4  procid, U2 index )
{
	if( procid == stDirectiveTblManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			stTable[ index-1] = NULL;
		}
	}
}
static TableType core_getTable( AST index )
{
	TableType temp = (TableType)Check_Com( &stDirectiveTblManager, resetListener,index );
	if( temp != NULL ){ return &temp[index%maxNumPerBlk]; } return NULL;
}
static BOOL ClearTable( VOID )
{
	BOOL bRet1 = ClearBuffer();
	BOOL bRet2 = Clear_Com( &stDirectiveTblManager );
	return bRet1 && bRet2 ;
}
static BOOL ResetTable( VOID )
{
	BOOL bRet1 = ResetBuffer();
	BOOL bRet2 = Reset_Com( &stDirectiveTblManager );
	return bRet1 && bRet2 ;
}
static BOOL AllocTable( AST astReqArg )
{
	return AllocTable_Com( &stDirectiveTblManager, astReqArg, resetListener );
}
static AST getMaxLineNum( VOID )
{
	return maxTable;
}
// buf
/*  get method */
static CHAR * getCommandStr( AST index )
{
	BufType pTbl = core_getBuf( index );
	if( pTbl == NULL )
	{
		return "";
	}
	return pTbl;
}
/* set method */
static AST  setCommandStr( PSZ str )
{
	S4 lenBuf = str_length(str);
	AST astHeaderNo;
	if( lenBuf > 0 && lenBuf < MemGetBlockSize( ))
	{
		if( AllocBuffer( lenBuf + 1 ) ) {
			lenBuf = Buflen - lenBuf;
			astHeaderNo = lenBuf/stDirectiveBufManager.astMaxNum_Block;
			strcpy( (PSZ)Buffer[astHeaderNo]+lenBuf%stDirectiveBufManager.astMaxNum_Block, str );
			*((PSZ)Buffer[astHeaderNo]+Buflen%stDirectiveBufManager.astMaxNum_Block) = '\0';
		}
		else
		{
			return 0;
		}
	}
	else
	{
		if ( 0 == AllocBuffer( lenBuf + 1 ) )
		{
			return 0;
		}
		lenBuf = Buflen;
	}
	return lenBuf;
}

//ACCESS_U4_FIELD kind;
static U4 getKind( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ulKind;
}
static U4 setKind( AST index, U4 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ulKind = value;
}
//ACCESS_IDX_FIELD idx_param;
static AST getIdxParam( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->unParam.astPPCondition;
}
static AST setIdxParam( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->unParam.astPPCondition = value;
}
//ACCESS_STR_FIELD str_param;
static CHAR* getStrParam( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return "";
	}
	return getCommandStr(pTbl->unParam.astErrorMsg);
}
static AST setStrParam( AST index, PSZ value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->unParam.astPPCondition = setCommandStr(value);
}

//ACCESS_S4_FIELD  s4_param;
static S4 getS4Param( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->lineno;
}
static S4 setS4Param( AST index, S4 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->lineno = value;
}
//ACCESS_BOOL_FIELD bool_result;
static BOOL getBoolResult( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->unExeResult.ulTrueFalse;
}
static BOOL setBoolResult( AST index, BOOL value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->unExeResult.ulTrueFalse = value;
}
//ACCESS_IDX_FIELD  idx_result;
static AST getIdxResult( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->unExeResult.astFileIndex;
}
static AST setIdxResult( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->unExeResult.astFileIndex = value;
}
//ACCESS_IDX_FIELD  locScope;

static AST getCodeScope( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astLocScope;
}
static AST setCodeScope( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astLocScope = value;
}
const DIRECTIVEINFOACCESS stCoreDirectiveInfoAccess = {
//		BOOL (*clear)( VOID );
//		BOOL (*reset)( VOID );
//		BOOL (*alloc)( AST );
//		AST  (*getMax)( VOID );
		ClearTable,
		ResetTable,
		AllocTable,
		getMaxLineNum,
//ACCESS_U4_FIELD kind;
		{
				getKind,
				setKind
		},
//ACCESS_IDX_FIELD idx_param;
		{
				getIdxParam,
				setIdxParam
		},
//ACCESS_STR_FIELD str_param;
		{
				getStrParam,
				setStrParam
		},
//ACCESS_S4_FIELD  s4_param;
		{
				getS4Param,
				setS4Param
		},
//ACCESS_BOOL_FIELD bool_result;
		{
				getBoolResult,
				setBoolResult
		},
//ACCESS_IDX_FIELD  idx_Result;
		{
				getIdxResult,
				setIdxResult
		},
//ACCESS_IDX_FIELD  locScope;
		{
			getCodeScope,
			setCodeScope

		}

};
